package com.example.fengyungo_user.utils;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.widget.Button;

/**
 * @author 徐
 * @describe 倒计时工具类
 */
public class CountDownTimerUtils extends CountDownTimer {

    private Button button;

    public CountDownTimerUtils(Button button, long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        this.button = button;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        // 设置不可点击
        button.setClickable(false);
        // 设置倒计时时间
        button.setText(millisUntilFinished / 1000 + "秒后可重新发送");
        // 获取按钮上的文字
        SpannableString spannableString = new SpannableString(button.getText().toString());
        ForegroundColorSpan span = new ForegroundColorSpan(Color.RED);
        // 将倒计时的时间设置为红色
        spannableString.setSpan(span, 0, 2, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        button.setText(spannableString);
        button.setTextSize(13);
    }

    @Override
    public void onFinish() {
        button.setText("重新获取验证码");
        // 重新获得点击
        button.setClickable(true);
    }
}